package com.huike.controller;

import com.huike.core.controller.BaseController;
import com.huike.core.domain.AjaxResult;
import com.huike.core.domain.R;
import com.huike.core.page.TableDataInfo;

import com.huike.dto.ContractDto;
import com.huike.pojo.Contract;
import com.huike.service.ContractService;
import com.huike.vo.ContractVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/contract")
@Slf4j
public class ContractController extends BaseController {

    @Autowired
    private ContractService contractService;

    /*查询合同分页的接口
     * */
    @GetMapping("/list")
    public TableDataInfo findContractAll(ContractDto contractDto) {
        startPage();
        List<Contract> contractList = contractService.selectContractListPage(contractDto);
        return getDataTable(contractList);
    }

    /*合同的新增接口
     * */
    @PostMapping
    public AjaxResult saveContract(@RequestBody Contract contract) {
        log.info("合同新增的参数,{}", contract);
        return toAjax(contractService.addContract(contract));

    }

    /*查询回显
     * */
    @GetMapping("/detail/{id}")
    public AjaxResult findInfo(@PathVariable("id") Long id) {
        log.info("获取查询合同的id: {}",id);
        Contract contract = contractService.selectContractById(id);
        return success(contract);
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove (@PathVariable Long id){
        log.info("获取当前合同的id，并删除: {}",id);

        return toAjax(contractService.deletById(id));
    }

    /*修改合同
     * */
    @PutMapping
    public AjaxResult update(@RequestBody Contract contract) {
        log.info("获取修改合同参数:{}" + contract);
        return toAjax(contractService.updateContract(contract));
    }

    /*根据id查看详情
     * */
    @GetMapping("/{id}")
    public R<ContractVo> getContract(@PathVariable Long id) {
        log.info("获得合同详情的参数:{}",id);
        ContractVo contractVo = contractService.getallByid(id);
        return R.ok(contractVo);

    }


}
